package com.examlpe.lotterysystem.service.activitystatus.operater;

import com.examlpe.lotterysystem.dao.dataobject.ActivityUserDO;
import com.examlpe.lotterysystem.dao.dataobject.UserDO;
import com.examlpe.lotterysystem.dao.mapper.ActivityUserMapper;
import com.examlpe.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.examlpe.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperater extends AbstractActivityOperater{

    @Autowired
    private ActivityUserMapper activityUserMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIDs();
        ActivityUserStatusEnum userStatus = convertActivityStatusDTO.getTargeUSerStatus();
        if(null == activityId
                || CollectionUtils.isEmpty(userIds)
                || null == userStatus) {
            return false;
        }
        List<ActivityUserDO> userDOList = activityUserMapper.bachSelectByAUIds(activityId,userIds);
        if(CollectionUtils.isEmpty(userDOList)) {
            return false;
        }
        for(ActivityUserDO auDO : userDOList) {
            if(userStatus.name().equalsIgnoreCase(auDO.getStatus())) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIDs();
        ActivityUserStatusEnum userStatus = convertActivityStatusDTO.getTargeUSerStatus();
        try{
            activityUserMapper.bachUpdateStatus(activityId,userIds,userStatus.name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
